<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>第3章 基本概念 | 我的世界</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/notes/favicon.ico">
    <link rel="manifest" href="/notes/manifest.json">
    <meta name="description" content="学习，生活，还有美食~">
    
    <link rel="preload" href="/notes/assets/css/0.styles.8453c794.css" as="style"><link rel="preload" href="/notes/assets/js/app.f19b5e39.js" as="script"><link rel="preload" href="/notes/assets/js/2.69392339.js" as="script"><link rel="preload" href="/notes/assets/js/44.ef1661d7.js" as="script"><link rel="preload" href="/notes/assets/js/14.e376f3f7.js" as="script"><link rel="prefetch" href="/notes/assets/js/10.4748ef2e.js"><link rel="prefetch" href="/notes/assets/js/11.4ec67c3b.js"><link rel="prefetch" href="/notes/assets/js/12.db234a0a.js"><link rel="prefetch" href="/notes/assets/js/13.0bdeeeea.js"><link rel="prefetch" href="/notes/assets/js/15.873f63d0.js"><link rel="prefetch" href="/notes/assets/js/16.6d650151.js"><link rel="prefetch" href="/notes/assets/js/17.f0bcb5c9.js"><link rel="prefetch" href="/notes/assets/js/18.7dffee3a.js"><link rel="prefetch" href="/notes/assets/js/19.5f0252c4.js"><link rel="prefetch" href="/notes/assets/js/20.abea2681.js"><link rel="prefetch" href="/notes/assets/js/21.252713aa.js"><link rel="prefetch" href="/notes/assets/js/22.c99488d0.js"><link rel="prefetch" href="/notes/assets/js/23.3839292f.js"><link rel="prefetch" href="/notes/assets/js/24.9cc2f325.js"><link rel="prefetch" href="/notes/assets/js/25.e9dcd2f1.js"><link rel="prefetch" href="/notes/assets/js/26.31898ae6.js"><link rel="prefetch" href="/notes/assets/js/27.88db2371.js"><link rel="prefetch" href="/notes/assets/js/28.2d29c56c.js"><link rel="prefetch" href="/notes/assets/js/29.a0b42251.js"><link rel="prefetch" href="/notes/assets/js/3.48b2b659.js"><link rel="prefetch" href="/notes/assets/js/30.9b38a2bb.js"><link rel="prefetch" href="/notes/assets/js/31.d374da8e.js"><link rel="prefetch" href="/notes/assets/js/32.7868c3f3.js"><link rel="prefetch" href="/notes/assets/js/33.87730e15.js"><link rel="prefetch" href="/notes/assets/js/34.5a5b6c6e.js"><link rel="prefetch" href="/notes/assets/js/35.1deedbd4.js"><link rel="prefetch" href="/notes/assets/js/36.d39f2b24.js"><link rel="prefetch" href="/notes/assets/js/37.d87637b4.js"><link rel="prefetch" href="/notes/assets/js/38.d118907c.js"><link rel="prefetch" href="/notes/assets/js/39.9f2c8514.js"><link rel="prefetch" href="/notes/assets/js/4.161aee82.js"><link rel="prefetch" href="/notes/assets/js/40.917feb30.js"><link rel="prefetch" href="/notes/assets/js/41.78e1b969.js"><link rel="prefetch" href="/notes/assets/js/42.cd6ac9d4.js"><link rel="prefetch" href="/notes/assets/js/43.961a438d.js"><link rel="prefetch" href="/notes/assets/js/45.b89cd059.js"><link rel="prefetch" href="/notes/assets/js/46.8ca9e560.js"><link rel="prefetch" href="/notes/assets/js/47.a35bec74.js"><link rel="prefetch" href="/notes/assets/js/48.953bb15e.js"><link rel="prefetch" href="/notes/assets/js/49.ae614c87.js"><link rel="prefetch" href="/notes/assets/js/5.ef4783db.js"><link rel="prefetch" href="/notes/assets/js/50.a9fbc190.js"><link rel="prefetch" href="/notes/assets/js/51.12ae367b.js"><link rel="prefetch" href="/notes/assets/js/52.57a5efe4.js"><link rel="prefetch" href="/notes/assets/js/53.861eb65b.js"><link rel="prefetch" href="/notes/assets/js/54.3bc1c9a1.js"><link rel="prefetch" href="/notes/assets/js/55.f724b26c.js"><link rel="prefetch" href="/notes/assets/js/56.02e48e7a.js"><link rel="prefetch" href="/notes/assets/js/6.72ed1ea6.js"><link rel="prefetch" href="/notes/assets/js/7.f1e39dec.js"><link rel="prefetch" href="/notes/assets/js/8.84b75613.js"><link rel="prefetch" href="/notes/assets/js/9.e1cd5390.js">
    <link rel="stylesheet" href="/notes/assets/css/0.styles.8453c794.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/notes/" class="home-link router-link-active"><img src="/notes/images/fish.gif" alt="我的世界" class="logo"> <span class="site-name can-hide">我的世界</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/notes/notes/" class="nav-link router-link-active">🎓 学习</a></div><div class="nav-item"><a href="/notes/life/" class="nav-link">🏸 生活</a></div> <a href="https://gitee.com/zgj6" target="_blank" rel="noopener noreferrer" class="repo-link">
    git
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/notes/notes/" class="nav-link router-link-active">🎓 学习</a></div><div class="nav-item"><a href="/notes/life/" class="nav-link">🏸 生活</a></div> <a href="https://gitee.com/zgj6" target="_blank" rel="noopener noreferrer" class="repo-link">
    git
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p title="学习笔记" class="sidebar-heading"><span>学习笔记</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="JavaScript 高程笔记" class="sidebar-heading open"><span>JavaScript 高程笔记</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/notes/notes/pro-js-for-web-dev-note/" aria-current="page" title="JavaScript 高级程序设计 · 笔记" class="sidebar-link">JavaScript 高级程序设计 · 笔记</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter01.html" title="第一章 JavaScript简介" class="sidebar-link">第一章 JavaScript简介</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter02.html" title="第二章 在HTML中使用JavaScript" class="sidebar-link">第二章 在HTML中使用JavaScript</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html" aria-current="page" title="第3章 基本概念" class="active sidebar-link">第3章 基本概念</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#语法" title="语法" class="sidebar-link">语法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#预解释" title="预解释" class="sidebar-link">预解释</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#数据类型" title="数据类型" class="sidebar-link">数据类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#undefined-类型" title="Undefined 类型" class="sidebar-link">Undefined 类型</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#null类型" title="Null类型" class="sidebar-link">Null类型</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#boolean类型" title="Boolean类型" class="sidebar-link">Boolean类型</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#number类型" title="Number类型" class="sidebar-link">Number类型</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#string-类型" title="String 类型" class="sidebar-link">String 类型</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#object类型" title="Object类型" class="sidebar-link">Object类型</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#操作符" title="操作符" class="sidebar-link">操作符</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#语句" title="语句" class="sidebar-link">语句</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#with语句" title="with语句" class="sidebar-link">with语句</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html#函数" title="函数" class="sidebar-link">函数</a></li></ul></li></ul></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter04.html" title="第四章 变量、作用域和内存问题" class="sidebar-link">第四章 变量、作用域和内存问题</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html" title="引用类型" class="sidebar-link">引用类型</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter06.html" title="面向对象的程序设计" class="sidebar-link">面向对象的程序设计</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p title="HTTP" class="sidebar-heading"><span>HTTP</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="微信小程序" class="sidebar-heading"><span>微信小程序</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="TS" class="sidebar-heading"><span>TS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="Vite" class="sidebar-heading"><span>Vite</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="interviewquestion" class="sidebar-heading"><span>interviewquestion</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="AI" class="sidebar-heading"><span>AI</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="UE" class="sidebar-heading"><span>UE</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="第3章-基本概念"><a href="#第3章-基本概念" class="header-anchor">#</a> 第3章 基本概念</h1> <h2 id="语法"><a href="#语法" class="header-anchor">#</a> 语法</h2> <p>JS变量区分大小写，其第一个字符必须是一个字母、下划线（_）、或美元符号（$）</p> <p>严格模式：在函数内部的上方包含这条编译指示，也可以指定函数在严格模式下执行：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">doSomething</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token string">&quot;use strict&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">//函数体</span>
<span class="token punctuation">}</span>
</code></pre></div><p>推荐在语句结尾加上分号<code>;</code>。
推荐在变量前面加上<code>var</code>标识符。</p> <h2 id="预解释"><a href="#预解释" class="header-anchor">#</a> 预解释</h2> <p>预解释是指浏览器在执行JS代码之前，对代码中的用<code>var</code>声明<strong>变量/对象</strong>以及<strong>函数</strong>（<code>function</code>）进行预处理的机制。
使用<code>var</code>声明的变量会在浏览器中进行预解释。不使用<code>var</code>声明的（全局的）变量不会预解释，这相当于给<code>window</code>增加了一个它的属性名（属性值）。
对<code>var</code>变量（对象）进行预解释时，仅进行声明，不会赋值，所以其值为<code>undefined</code>。
函数<code>function</code>在预解释时不仅会声明，并且会赋值，复制的内容是函数字符串在内存中的引用。所以函数可以在代码的任意地方调用。</p> <h2 id="数据类型"><a href="#数据类型" class="header-anchor">#</a> 数据类型</h2> <p>JS包含了5种<strong>简单</strong>数据类型：<code>Undefined</code>、<code>Null</code>、<code>Boolean</code>、<code>Number</code>、<code>String</code>；还有一种<strong>复杂</strong>数据类型<code>Object</code>。</p> <p><code>Object</code>类型本质上是由一组无序的键值对组成的。
JS不支持创建自定义数据类型，因此只有6种数据类型。</p> <p>JS是松散类型的，需要有一种能检测变量数据类型手段，他就是<code>typeof</code>。使用typeof可以返回一些字符串：</p> <ul><li>&quot;undefined&quot; 这个值未定义</li> <li>&quot;boolean&quot; 是布尔值</li> <li>&quot;string&quot; 是字符串</li> <li>&quot;number&quot; 是数值</li> <li>&quot;object&quot; 是对象或null</li> <li>&quot;function&quot; 是函数</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> message <span class="token operator">=</span> <span class="token string">&quot;some string&quot;</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// &quot;string&quot;</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">typeof</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// &quot;string&quot;</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token number">95</span><span class="token punctuation">)</span><span class="token punctuation">;</span>          <span class="token comment">// &quot;number&quot;</span>
</code></pre></div><h3 id="undefined-类型"><a href="#undefined-类型" class="header-anchor">#</a> Undefined 类型</h3> <p>Undefined类型只有一个值：undefined。在使用var声明变量但未对其初始化时，就是undefined类型。</p> <p>对未初始化的变量执行 typeof 操作符会返回 undefined 值，而对未声明的变量执行 typeof 操作符同样也会返回 undefined 值。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// &quot;undefined&quot; </span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> age<span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">// &quot;undefined&quot; </span>
</code></pre></div><p>显式地初始化变量依然是明智的选择。如果能够做到这一点，那么当 typeof 操作符返回&quot;undefined&quot;值时，我们就知道被检测的变量还没有被声明，而不是尚未初始化。</p> <h3 id="null类型"><a href="#null类型" class="header-anchor">#</a> Null类型</h3> <p>Null类型是第二个只有一个值的数据类型，null。null表示一个空对象指针，所以<code>typeof null</code>返回的值是<code>&quot;object&quot;</code>。</p> <p>如果定义的变量准备在将来用于保存对象，那么最好将该变量初始化为 null 而不是其他值。这样一来，只要直接检查 null值就可以知道相应的变量是否已经保存了一个对象的引用。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>car <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 操作</span>
<span class="token punctuation">}</span>
</code></pre></div><p>实际上，undefined 值是派生自 null 值的，因此 ECMA-262 规定对它们的相等性测试要返回 true。</p> <p><code>undefined == null // true</code></p> <p><img src="http://0.rc.xiniu.com/g2/M00/73/2F/CgAGe1rMqRSAIdXZAAAKCBlGPrU502.png" alt="image"></p> <h3 id="boolean类型"><a href="#boolean类型" class="header-anchor">#</a> Boolean类型</h3> <p>Boolean类型只有两个类型，<code>true</code>和<code>false</code>，这两个值与0和1无关。</p> <p>Boolean()方法，用于转换参数为Boolean值。</p> <table><thead><tr><th>数据类型</th> <th>转换为true的值</th> <th>转换为false的值</th></tr></thead> <tbody><tr><td>Boolean</td> <td>true</td> <td>false</td></tr> <tr><td>String</td> <td>任何非空字符串</td> <td>&quot;&quot;（空字符串）</td></tr> <tr><td>Number</td> <td>任何非零数字（包括无穷大）</td> <td>0和NaN</td></tr> <tr><td>Object</td> <td>任何对象</td> <td>null</td></tr> <tr><td>Undefined</td> <td>n/a*</td> <td>undefined</td></tr></tbody></table> <p>*n/a（或 N/A），是 not applicable的缩写，意思是“不适用”。</p> <h3 id="number类型"><a href="#number类型" class="header-anchor">#</a> Number类型</h3> <p>Number类型用于表示整数和浮点数。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> intNum <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>     <span class="token comment">// 十进制</span>
<span class="token keyword">var</span> octalNum <span class="token operator">=</span> <span class="token number">070</span><span class="token punctuation">;</span> <span class="token comment">// 八进制（56）</span>
<span class="token keyword">var</span> hexNum <span class="token operator">=</span> <span class="token number">0x1f</span><span class="token punctuation">;</span>  <span class="token comment">// 十六进制（31）</span>
</code></pre></div><p>Number类型可以保存<code>0</code>和<code>-0</code>，正零和负零被认为相等。</p> <p>最大值、最小值与负无穷、正无穷</p> <div class="language-js extra-class"><pre class="language-js"><code>Number<span class="token punctuation">.</span><span class="token constant">MIN_VALUE</span>
<span class="token comment">// 5e-324</span>
Number<span class="token punctuation">.</span><span class="token constant">MAX_VALUE</span>
<span class="token comment">// 1.7976931348623157e+308</span>
Number<span class="token punctuation">.</span><span class="token constant">NEGATIVE_INFINITY</span>
<span class="token comment">// -Infinity</span>
Number<span class="token punctuation">.</span><span class="token constant">POSITIVE_INFINITY</span>
<span class="token comment">// Infinity</span>
</code></pre></div><p>NaN（非数值）是一个特殊的数值。NaN与任何值都不相等</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token number">0</span><span class="token operator">/</span><span class="token number">0</span>     <span class="token comment">// NaN</span>
<span class="token number">1</span><span class="token operator">/</span><span class="token number">0</span>     <span class="token comment">// Infinity</span>
<span class="token operator">-</span><span class="token number">1</span><span class="token operator">/</span><span class="token number">0</span>          <span class="token comment">// -Infinity</span>
<span class="token number">NaN</span> <span class="token operator">==</span> <span class="token number">NaN</span>    <span class="token comment">// false</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token function">alert</span><span class="token punctuation">(</span><span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">//true</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>          <span class="token comment">//false（10 是一个数值）</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token string">&quot;10&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment">//false（可以被转换成数值 10）</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token string">&quot;blue&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//true（不能转换成数值）</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment">//false（可以被转换成数值 1）</span>
</code></pre></div><h4 id="数值转换"><a href="#数值转换" class="header-anchor">#</a> 数值转换</h4> <p>有 3 个函数可以把非数值转换为数值：Number()、parseInt()和 parseFloat()。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> num1 <span class="token operator">=</span> <span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">&quot;Hello world!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//NaN </span>
<span class="token keyword">var</span> num2 <span class="token operator">=</span> <span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                  <span class="token comment">//0 </span>
<span class="token keyword">var</span> num3 <span class="token operator">=</span> <span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">&quot;000011&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">//11 </span>
<span class="token keyword">var</span> num4 <span class="token operator">=</span> <span class="token function">Number</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>               <span class="token comment">//1 </span>
</code></pre></div><p>不指定基数意味着让 parseInt()决定如何解析输入的字符串，因此为了避免错误的解析，我们建
议无论在什么情况下都明确指定基数。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> num1 <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;10&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//2  （按二进制解析） </span>
<span class="token keyword">var</span> num2 <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;10&quot;</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">//8  （按八进制解析） </span>
<span class="token keyword">var</span> num3 <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;10&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//10 （按十进制解析） </span>
<span class="token keyword">var</span> num4 <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;10&quot;</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//16 （按十六进制解析）</span>
</code></pre></div><p>parseFloat()也是从第一个字符（位置 0）开始解析每个字符。而且
也是一直解析到字符串末尾，或者解析到遇见一个无效的浮点数字字符为止。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> num1 <span class="token operator">=</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">&quot;1234blue&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">//1234 （整数） </span>
<span class="token keyword">var</span> num2 <span class="token operator">=</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">&quot;0xA&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>               <span class="token comment">//0 </span>
<span class="token keyword">var</span> num3 <span class="token operator">=</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">&quot;22.5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>              <span class="token comment">//22.5 </span>
<span class="token keyword">var</span> num4 <span class="token operator">=</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">&quot;22.34.5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">//22.34 </span>
<span class="token keyword">var</span> num5 <span class="token operator">=</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">&quot;0908.5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">//908.5 </span>
<span class="token keyword">var</span> num6 <span class="token operator">=</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">&quot;3.125e7&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">//31250000 </span>
</code></pre></div><h3 id="string-类型"><a href="#string-类型" class="header-anchor">#</a> String 类型</h3> <p>String 类型用于表示由零或多个 16 位 Unicode 字符组成的字符序列，即字符串。字符串可以由双
引号（&quot;）或单引号（'）表示。</p> <p>ECMAScript 中的字符串是不可变的，也就是说，字符串一旦创建，它们的值就不能改变。要改变
某个变量保存的字符串，首先要销毁原来的字符串，然后再用另一个包含新值的字符串填充该变量。</p> <p>转换成字符串</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> age <span class="token operator">=</span> <span class="token number">11</span><span class="token punctuation">;</span> 
<span class="token keyword">var</span> ageAsString <span class="token operator">=</span> age<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">// 字符串&quot;11&quot; </span>
<span class="token keyword">var</span> found <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> 
<span class="token keyword">var</span> foundAsString <span class="token operator">=</span> found<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// 字符串&quot;true&quot;</span>
</code></pre></div><p>toString()可以输出以二进制、八进制、十六进制，乃至其他任意有效进制格
式表示的字符串值。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span> 
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">// &quot;10&quot; </span>
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment">// &quot;1010&quot; </span>
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment">// &quot;12&quot; </span>
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// &quot;10&quot; </span>
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// &quot;a&quot; </span>
</code></pre></div><p>String()方法将任何类型的值转换成字符串</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">String</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>        <span class="token comment">// &quot;true&quot;</span>
<span class="token function">String</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span>         <span class="token comment">// &quot;123&quot;</span>
<span class="token function">String</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span>        <span class="token comment">// &quot;null&quot;</span>
<span class="token function">String</span><span class="token punctuation">(</span><span class="token keyword">undefined</span><span class="token punctuation">)</span>   <span class="token comment">// &quot;undefined&quot;</span>
<span class="token function">String</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span>         <span class="token comment">// &quot;NaN&quot;</span>
</code></pre></div><h3 id="object类型"><a href="#object类型" class="header-anchor">#</a> Object类型</h3> <p>ECMAScript 中的对象其实就是一组数据和功能的集合。</p> <p>创建一个对象：<code>var o = new Object();</code>。Object 类型是它所有实例的基础</p> <p>Object的每个实例都有以下属性和方法</p> <ul><li>constructor</li> <li>hasOwnProperty(propertyName)</li> <li>isPrototypeOf(object)</li> <li>propertyIsEnumerable(propertyName)</li> <li>toLocaleString()</li> <li>toString()</li> <li>valueOf()</li></ul> <p>宿主对象：DOM和BOM中的对象。</p> <p><code>toLocaleString()</code>主要将数组Number对象或Date对象转换成本地字符串</p> <div class="language-js extra-class"><pre class="language-js"><code>today<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// &quot;Sat May 05 2018 16:01:33 GMT+0800&quot;</span>
today<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// &quot;2018/5/5 下午4:01:33&quot;</span>
money<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// &quot;1,234.56&quot;</span>
money<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// &quot;1234.56&quot;</span>
</code></pre></div><h3 id="操作符"><a href="#操作符" class="header-anchor">#</a> 操作符</h3> <h4 id="一元操作符"><a href="#一元操作符" class="header-anchor">#</a> 一元操作符：<code>++</code> , <code>--</code></h4> <h4 id="位操作符-暂略"><a href="#位操作符-暂略" class="header-anchor">#</a> 位操作符：暂略</h4> <h4 id="布尔操作符"><a href="#布尔操作符" class="header-anchor">#</a> 布尔操作符:<code>!</code> , <code>&amp;&amp;</code> , <code>||</code></h4> <p>我们可以利用逻辑或的这一行为来避免为变量赋 null 或 undefined 值。例如：</p> <p><code>var myObject = preferredObject || backupObject;</code></p> <h4 id="乘性操作符"><a href="#乘性操作符" class="header-anchor">#</a> 乘性操作符：<code>*</code> , <code>/</code> , <code>%</code></h4> <h4 id="加性操作符"><a href="#加性操作符" class="header-anchor">#</a> 加性操作符：<code>+</code>, <code>-</code></h4> <h4 id="关系操作符"><a href="#关系操作符" class="header-anchor">#</a> 关系操作符：<code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code>, <code>&gt;=</code></h4> <p>在比较字符串时，实际比较的是两个字符串中对应位置的每个字符的字符编码值。</p> <p>任何操作数与<code>NaN</code>比较，结果都是false</p> <h4 id="相等操作符"><a href="#相等操作符" class="header-anchor">#</a> 相等操作符：<code>==</code>, <code>!=</code>, <code>===</code>, <code>!===</code></h4> <p>注意，<code>==</code>和<code>!=</code>操作符会强制转换操作数类型，然后再判断他们是否相等</p> <p>在比较布尔值时，将其转换成数值。null和undefined是相等的。NaN不与任何值相等，也不与它本身相等。如果操作数都是对象则比较它们是否同一个对象。</p> <p>全等/全不等操作符<code>===</code> / <code>!===</code>不会进行强制类型转换。推荐使用这种操作符。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token string">'55'</span> <span class="token operator">==</span> <span class="token number">55</span>  <span class="token comment">//true</span>
<span class="token string">'55'</span> <span class="token operator">===</span> <span class="token number">55</span> <span class="token comment">//false</span>
</code></pre></div><h4 id="条件操作符"><a href="#条件操作符" class="header-anchor">#</a> 条件操作符</h4> <p>遵循与Java中相同的语法格式。</p> <div class="language-js extra-class"><pre class="language-js"><code>variable <span class="token operator">=</span> boolean_expression <span class="token operator">?</span> true_value <span class="token operator">:</span> false_value<span class="token punctuation">;</span>
<span class="token keyword">var</span> max <span class="token operator">=</span> <span class="token punctuation">(</span>num1 <span class="token operator">&gt;</span> num2<span class="token punctuation">)</span> <span class="token operator">?</span> num1 <span class="token operator">:</span> num2<span class="token punctuation">;</span> 
</code></pre></div><h4 id="赋值操作符"><a href="#赋值操作符" class="header-anchor">#</a> 赋值操作符：<code>=</code>, <code>+=</code>, <code>-=</code>...</h4> <h4 id="逗号操作符"><a href="#逗号操作符" class="header-anchor">#</a> 逗号操作符：<code>,</code></h4> <p>逗号操作符可以在一条语句中执行多个操作。逗号操作符总会返回 表达式的最后一项。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> num1<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> num2<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> num3<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// num 的值为 0</span>
</code></pre></div><h3 id="语句"><a href="#语句" class="header-anchor">#</a> 语句</h3> <h4 id="if语句"><a href="#if语句" class="header-anchor">#</a> if语句</h4> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">&gt;</span> <span class="token number">25</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&quot;Greater than 25.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&quot;Less than 0.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> 
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&quot;Between 0 and 25, inclusive.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre></div><h4 id="do-while"><a href="#do-while" class="header-anchor">#</a> do-while</h4> <p>循环语句，内容至少循环一次</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 
<span class="token keyword">do</span> <span class="token punctuation">{</span> 
   i <span class="token operator">+=</span> <span class="token number">2</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token function">alert</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> 
</code></pre></div><h4 id="while"><a href="#while" class="header-anchor">#</a> while</h4> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>i<span class="token operator">&lt;</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    i <span class="token operator">+=</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="for"><a href="#for" class="header-anchor">#</a> for</h4> <p>使用 while 循环做不到的，使用 for 循环同样也做不到。也就是说，for 循环只是把与循环有关
的代码集中在了一个位置。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="for-in"><a href="#for-in" class="header-anchor">#</a> for-in</h4> <p>for-in 语句是一种精准的迭代语句，可以用来枚举对象的属性。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> propName <span class="token keyword">in</span> window<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>propName<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="label语句"><a href="#label语句" class="header-anchor">#</a> label语句</h4> <p>这个例子中定义的 start 标签可以在将来由 break 或 continue 语句引用。</p> <div class="language-js extra-class"><pre class="language-js"><code>start<span class="token operator">:</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
    <span class="token function">alert</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>  
<span class="token punctuation">}</span> 
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
test<span class="token operator">:</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'i='</span><span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'     j='</span><span class="token operator">+</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>i<span class="token operator">==</span><span class="token number">5</span> <span class="token operator">&amp;&amp;</span> j<span class="token operator">==</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">break</span> test<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    num<span class="token operator">++</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="break和continue"><a href="#break和continue" class="header-anchor">#</a> break和continue</h4> <p>break会立即退出循环执行循环后面的语句。continue也是立即退出循环，但退出循环后会从循环顶部继续执行。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
    <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">5</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
       <span class="token keyword">break</span><span class="token punctuation">;</span> 
    <span class="token punctuation">}</span> 
    num<span class="token operator">++</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
 
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//4 </span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
    <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">5</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
        <span class="token keyword">continue</span><span class="token punctuation">;</span> 
    <span class="token punctuation">}</span> 
    num<span class="token operator">++</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
 
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//8 </span>
</code></pre></div><h3 id="with语句"><a href="#with语句" class="header-anchor">#</a> with语句</h3> <p>定义 with 语句的目的主要是为了简化多次编写同一个对象的工作。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> qs <span class="token operator">=</span> location<span class="token punctuation">.</span>search<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> hostName <span class="token operator">=</span> location<span class="token punctuation">.</span>hostname<span class="token punctuation">;</span>
<span class="token keyword">var</span> url <span class="token operator">=</span> location<span class="token punctuation">.</span>href<span class="token punctuation">;</span>
</code></pre></div><p>简化成</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">with</span><span class="token punctuation">(</span>location<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> qs <span class="token operator">=</span> search<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> hostName <span class="token operator">=</span> hostname<span class="token punctuation">;</span>
    <span class="token keyword">var</span> url <span class="token operator">=</span> href<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>由于大量使用 with 语句会导致性能下降，同时也会给调试代码造成困难，因此
在开发大型应用程序时，不建议使用 with 语句。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">switch</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">'string1'</span><span class="token operator">:</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'case1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">234</span><span class="token operator">:</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'case2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>switch语句中可以使用任何数据类型。每个case的值不一定是常量，可以是变量甚至是表达式。</p> <h3 id="函数"><a href="#函数" class="header-anchor">#</a> 函数</h3> <p>通过函数可以封装任意多条语句，而且可以在任何地方、任何时候执行。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">helloWorld</span><span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'Hello world! '</span><span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">diff</span><span class="token punctuation">(</span><span class="token parameter">num1<span class="token punctuation">,</span> num2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>num1 <span class="token operator">&gt;</span> num2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> num1 <span class="token operator">-</span> num2<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> num2 <span class="token operator">-</span> num1<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>不能把函数或参数命名为<code>eval</code>或<code>argument</code>！</li> <li>如果函数不带返回值，则默认返回<code>undefined</code>。</li> <li>函数不介意传入多少参数，即便你定义的函数只接收两个参数，在调用这个函数时也未必一定要传递两个参数。可以传递一个、三个甚至不传递参数。</li> <li>函数中的参数在内是用一个数组表示的，因此可以用方括号语法访问它的每一个元素。</li> <li>ECMAScript 中的所有参数传递的都是值（复制），不可能通过引用传递参数。</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">myFunction</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> len <span class="token operator">=</span> arguments<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>len<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>len<span class="token operator">+</span><span class="token string">': '</span><span class="token operator">+</span>arguments<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'没有参数'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token parameter">arg0<span class="token punctuation">,</span> arg1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  arguments<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>arguments<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">test</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>  <span class="token comment">// 10</span>
<span class="token function">alert</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>    <span class="token comment">// 6</span>
</code></pre></div><h4 id="函数没有重载"><a href="#函数没有重载" class="header-anchor">#</a> 函数没有重载</h4> <ul><li>ECMAScript 函数不能像传统意义上（如Java）那样实现重载。</li> <li>ECMAScirpt 函数没有签名</li> <li>如果在 ECMAScript 中定义了两个名字相同的函数，则该名字只属于后定义的函数。</li></ul></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-4-5 17:45:19</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/notes/notes/pro-js-for-web-dev-note/chapter02.html" class="prev">第二章 在HTML中使用JavaScript</a></span> <span class="next"><a href="/notes/notes/pro-js-for-web-dev-note/chapter04.html">第四章 变量、作用域和内存问题</a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><div id="live2d-widget" class="live2d-widget-container" style="position:fixed;right:65px;bottom:0px;width:135px;height:300px;z-index:99999;opacity:0.8;pointer-events:none;"><canvas id="live2d_canvas" width="135" height="300" class="live2d_canvas" style="position:absolute;left:0px;top:0px;width:135px;height:300px;"></canvas></div></div></div>
    <script src="/notes/assets/js/app.f19b5e39.js" defer></script><script src="/notes/assets/js/2.69392339.js" defer></script><script src="/notes/assets/js/44.ef1661d7.js" defer></script><script src="/notes/assets/js/14.e376f3f7.js" defer></script>
  </body>
</html>
